package com.lge.qcontact.configuration;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.lge.qcontact.MainActivity;
import com.lge.qcontact.R;
import com.lge.qcontact.utils.LocalDBHelper;
import com.lge.qcontact.utils.MUtils;

public class DropOutActivity extends Activity {
	private EditText idField;
	private EditText pwField;
	private Button dropBtn;
	private SQLiteDatabase localDB;
	private String userId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_dropout);
		
		idField = (EditText) findViewById(R.id.dropIDField);
		pwField = (EditText) findViewById(R.id.dropPWField);
		
		dropBtn = (Button) findViewById(R.id.dropBtn);
		dropBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				AlertDialog.Builder builder = new AlertDialog.Builder(DropOutActivity.this);
				builder.setTitle("주의");
				builder.setMessage("탈퇴하시면 모든 데이터가 삭제됩니다.\n탈퇴 하시겠습니까?");
				builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						Cursor c = localDB.rawQuery("select uid from LOGIN_INFO;", null);
						c.moveToNext();
						userId = c.getString(0);
						
						DeleteThread t = new DeleteThread();
						t.start();
						try {
							t.join();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						localDB.execSQL("delete from CONTACTS;");
						localDB.execSQL("delete from LOGIN_INFO;");
						
						MUtils.toast(getBaseContext(), "탈퇴가 완료되었습니다.");
						finish();
					}
				});
				builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				builder.show();
			}
		});
		
		
		
		localDB = (new LocalDBHelper(this)).getReadableDatabase();
		
		Cursor c = localDB.rawQuery("select * from LOGIN_INFO;", null);
		
		c.moveToNext();
		
		String uId = c.getString(0);
		String uPw = c.getString(1);
		
		
		if(uId !=null && uPw != null){
			idField.setText(uId);
			idField.setEnabled(false);
			pwField.setText(uPw);
			pwField.setEnabled(false);
		}
	}
	
	class DeleteThread extends Thread{

		@Override
		public void run() {
			StringBuffer sql = new StringBuffer("http://192.168.0.205:8080/com.lg.project.myexample.cmd.DeleteMemberCmd.vcc?param="+userId);
			
			try {
				URL url = new URL(sql.toString());
				url.openStream();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		

		

	}
}
